using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace NewFeatures4 { public class Order { public Order() { cust = new Customer("Brian", "Jones", 27); } //This method returns a Customer but as an Object public Object GetCustomer() { return cust; } Customer cust; } //The Customer class class Dynamic { static void Main(string[] args) { Order ord = new Order(); //Declare a dynamic type dynamic cust = ord.GetCustomer(); //A dynamic type prevents compiler type checking cust.FirstName = "Paul"; // works as expected cust.DisplayCustomer(); // works as expected //cust.MissingMethod(); // No method exists but no build error int woo = 10; // Declare an integer //woo = woo / 2.5; //Error: cannot convert integer to decimal dynamic foo = 10; //This time use a dynamic type foo = foo / 2.5; //The runtime takes care of type conversion foo = "John"; //And again Console.WriteLine(foo); Console.ReadLine(); } } public class Customer { // properties public string FirstName { get; set; } public string LastName { get; set; } public int Age { get; set; } // Constructor public Customer(string strFirstName, string strLastName, int intAge) { FirstName = strFirstName; LastName = strLastName; Age = intAge; } // a method to display the date public void DisplayCustomer() { Console.WriteLine(String.Format("{0} {1} {2}", FirstName, LastName, Age)); } } }